package com.wolf.controller;

import com.wolf.pojo.Wl_commodity_inventory_record;
import com.wolf.service.Wl_commodity_inventory_recordService;
import com.wolf.util.execl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

@Controller
@ComponentScan
public class Wl_commodity_inventory_recordController {

    @Autowired
    private Wl_commodity_inventory_recordService wl_commodity_inventory_recordService;

    public void setWl_commodity_inventory_recordService(Wl_commodity_inventory_recordService wl_commodity_inventory_recordService) {
        this.wl_commodity_inventory_recordService = wl_commodity_inventory_recordService;
    }

    /**
     * 查询库存预警中的前100条数据
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_commodity_inventory_recordAll")
    public String getAll(ModelMap modelMap, HttpServletRequest q){

        List<Wl_commodity_inventory_record> list =  wl_commodity_inventory_recordService.getAll();
        for (Wl_commodity_inventory_record w:list) {
            if(w.getProperty()==null||w.getProperty()==""){
                continue;
            }
            JSONObject obj = JSONObject.fromObject(w.getProperty());

            //迭代器
            Iterator it = obj.keys();
            //标识
            int index = 1;
            while (it.hasNext()){
                if(index==1){
                    w.setProperty(null);
                    index=0;
                }
                //获取json中的key
                String key = it.next().toString();
                //获取json中的value
                String value = obj.getString(key);
                //把key和value重新拼接
                if(w.getProperty()==null){
                    w.setProperty(key+":"+value);
                }else{
                    w.setProperty(w.getProperty()+";"+key+":"+value);
                }
            }
        }

        modelMap.addAttribute("wl_commodity_inventory_record_list",list);
        q.getSession().setAttribute("list_execl",list);
        return "table_wl_commodity_inventory_record";
    }

    @RequestMapping("wl_commodity_inventory_record_getselective")
    public String getSelective(HttpServletRequest q,ModelMap modelMap,String commodityname,String property,Integer istatus,Integer rstatus,Integer month){

        if(commodityname=="" || commodityname==null){
            commodityname = null;
        }
        if(property=="" || property==null){
            property = null;
        }

        if(istatus==2){
            istatus=null;
        }
        if(rstatus==2){
            rstatus=null;
        }
        if(month==0){
            month=null;
        }

        List<Wl_commodity_inventory_record> list = wl_commodity_inventory_recordService.getSelective(commodityname,property,istatus,rstatus,month);

        for (Wl_commodity_inventory_record w:list) {
            if(w.getProperty()==null||w.getProperty()==""){
                continue;
            }
            JSONObject obj = JSONObject.fromObject(w.getProperty());

            //迭代器
            Iterator it = obj.keys();
            //标识
            int index = 1;
            while (it.hasNext()){
                if(index==1){
                    w.setProperty(null);
                    index=0;
                }
                //获取json中的key
                String key = it.next().toString();
                //获取json中的value
                String value = obj.getString(key);
                //把key和value重新拼接
                if(w.getProperty()==null){
                    w.setProperty(key+":"+value);
                }else{
                    w.setProperty(w.getProperty()+";"+key+":"+value);
                }
            }
        }

        modelMap.addAttribute("wl_commodity_inventory_record_list",list);
        q.getSession().setAttribute("list_execl",list);

        return "table_wl_commodity_inventory_record";
    }

    /**
     * 导出Excel表格
     * @param q
     * @return
     */
    @RequestMapping("getExecl")
    public String getExecl(HttpServletRequest q){

        List<Wl_commodity_inventory_record> list = (List<Wl_commodity_inventory_record>) q.getSession().getAttribute("list_execl");

        execl.text(list);

        return "table_wl_commodity_inventory_record";
    }
}
